<template>
<div id="home">
    <el-container class="container">
        <!-- 头部区域 -->
        <el-header class="header">
            <div>
                <img src="../../assets/logo.png" alt="">
                <span>电商后台管理系统</span>
            </div>
            <el-button type="info" @click="handleSignout">退出</el-button>
        </el-header>
        <!-- 页面主体 -->
        <el-container>
            <!-- 侧边栏 -->
            <el-aside :width="isCollapse ? '64px' : '200px'">
                <div class="toggle-button" @click="toggleCollapse">|||</div>
                <!-- 侧边栏导航 -->
                <el-menu :default-active="activePath" :unique-opened="true" :router="true" background-color="#545c64" text-color="#fff" active-text-color="#409EFF" :collapse="isCollapse" collapse-transition="false">
                    <!-- 一级菜单 -->
                    <el-submenu v-for="(item1) in menus" :key="item1.id" :index=" ''+item1.order">
                        <!-- 一级菜单模板区域 -->
                        <template slot="title">
                            <!-- 图标 -->
                            <i :class="iconObj[item1.id]"></i>
                            <!-- 文本 -->
                            <span>{{item1.authName}}</span>
                        </template>
                        <!-- 二级菜单 -->
                        <el-menu-item v-for="(item2,index) in item1.children" :key="index" :index="'/'+item2.path" @click="saveNavState('/'+item2.path)">
                            <!-- 图标 -->
                            <i class="el-icon-menu"></i>
                            <!-- 文本 -->
                            <span>{{item2.authName}}</span>
                        </el-menu-item>
                    </el-submenu>
                </el-menu>
            </el-aside>
            <!-- 右边内容展示 -->
            <el-main class="main">
                <router-view></router-view>
            </el-main>
        </el-container>
    </el-container>
</div>
</template>

<script>
export default {
    data() {
        return {
            menus: [],
            iconObj: {
                '125': 'iconfont icon-user',
                '103': 'iconfont icon-tijikongjian',
                '101': 'iconfont icon-shangpin',
                '102': 'iconfont icon-danju',
                '145': 'iconfont icon-baobiao'
            },
            isCollapse:false,
            // 被激活的链接地址
            activePath:''
        }
    },
    beforeCreate() {
        const token = localStorage.getItem('token')
        if (!token) {
            this.$router.push({
                name: 'login'
            })
        }
    },
    created() {
        this.getMenus(),
        this.activePath = window.sessionStorage.getItem('activePath')
    },
    methods: {
        //获取导航数据
        async getMenus() {
            const res = await this.$http.get(`menus`)
            this.menus = res.data.data
        },
        handleSignout() {
            // 清除token
            window.sessionStorage.clear()
            // 提示退出成功
            this.$message.success('退出成功！')
            // 跳转到登录页
            this.$router.push({
                name: 'login'
            })
        },
        toggleCollapse(){
            this.isCollapse = !this.isCollapse
        },
        //保存链接的激活状态
        saveNavState(activePath){
            window.sessionStorage.setItem('activePath',activePath)
            this.activePath = activePath
        }
    }
}
</script>

<style lang="less" scoped>
#home {
    height: 100%;
}

.container {
    height: 100%;
}

.header {
    background-color: #373d41;
    display: flex;
    justify-content: space-between;
    padding-left: 0;
    align-items: center;
    color: #fff;
    font-size: 20px;

    >div {
        display: flex;
        height: 100%;
        align-items: center;

        >img {
            height: 100%;
        }

        >span {
            margin-left: 20px;
        }
    }
}

.el-aside {
    background-color: #333744;
}
.iconfont{
    margin-right: 10px;
}
.toggle-button{
    background-color: #4a5064;
    font-size: 10px;
    line-height: 24px;
    color: #fff;
    text-align: center;
    letter-spacing: 0.2em;
    cursor: pointer;
}
</style>
